using Infrastructure.Attribute;
using SqlSugar;
using ZR.Model;
using ZR.Model.System;
using ZR.Model.System.Dto;
using ZR.ServiceCore.Services.IService;

namespace ZR.ServiceCore.Services
{
    /// <summary>
    /// 报警列表服务实现
    /// </summary>
    [AppService(ServiceType = typeof(IAlarmListService), ServiceLifetime = LifeTime.Transient)]
    public class AlarmListService : BaseService<AlarmList>, IAlarmListService
    {
        /// <inheritdoc/>
        public PagedInfo<AlarmList> GetAlarmList(AlarmListQueryDto query, PagerInfo pager)
        {
            var exp = Expressionable.Create<AlarmList>();

            if (!string.IsNullOrEmpty(query.Type))
            {
                exp.And(it => it.Type == query.Type);
            }

            if (!string.IsNullOrEmpty(query.City))
            {
                exp.And(it => it.City == query.City);
            }

            var total = 0;
            var list = Context.Queryable<AlarmList>()
                .Where(exp.ToExpression())
                .OrderByDescending(it => it.CreateTime)
                .ToPageList(pager.PageNum, pager.PageSize, ref total);

            return new PagedInfo<AlarmList>
            {
                TotalNum = total,
                Result = list,
                PageIndex = pager.PageNum,
                PageSize = pager.PageSize
            };
        }

        /// <inheritdoc/>
        public int AddAlarm(AlarmList alarm)
        {
            alarm.CreateTime = DateTime.Now;
            alarm.UpdateTime = DateTime.Now;
            return InsertReturnIdentity(alarm);
        }

        /// <inheritdoc/>
        public int DeleteAlarms(IEnumerable<int> ids)
        {
            return Delete(ids.ToArray());
        }
    }
}

